<?
//namespace nz\co\innovanet\fta\FTARegisteredUserSession;
include_once("cmsConfig.php");


	class FTARegisteredUserSession extends FTAObject {
	
		public $loginPage;
		public $loginSuccessPageUrl;
		
		function __construct() {
			parent::__construct();
		}
		
		/**
			returns true if we have logged in / registered session
		*/
		function hasSession() {
			return true;//false;// the default implementation will return true
		}
		
		function redirectToLoginPage(){
			//we will call redirectCleanly .. which will be included from somewhere and loaded.
			// we will then redirect magically to a specified page.
			redirectCleanly($this->loginPage);
		}
		
		function logout(){
			//we will call redirectCleanly .. which will be included from somewhere and loaded.
			// we will then redirect magically to a specified page.
			session_destroy();
			redirectCleanly($this->loginPage);
		}
				
		
		function redirectToSuccessPage() {
			redirectCleanly($this->loginSuccessPageUrl);
		}
	}
	
	
// HELPER METHODS FOR JAMIE

	/**
		this is a helper method that will password protect a page, if the session does not
		meet specigic registration / login criteria it will redirect to a log in page.
	*/
	function fta_passwordProtectPage(){
		// this will check the session for a class / object that provides the given authentication information.
		// all auth/session/public registrations.. should call a subclass of 
		//	
		//	FTARegisteredUserSession
		//
		// This function will check the session for an instance of FTARegisteredUserSession
		// and invoke the method hasSession
		// if there is no FTARegisteredUserSession or the hasSession fails
		// we will call the redirectToLoginPage method.
		// if the log in is successful we will, if we are the log in page, redirect to a specified page
		// or just return true..

		
		$o = &fta_getSharedRegisteredUserSession();
		
		if ($o->hasSession()) {
			
			if ($_SERVER['REQUEST_URI'] == $o->loginPage) {
				$o->redirectToSuccessPage();
			} else {}
		} else {	
			$o->redirectToLoginPage();			
		}
					
	}
	
	function fta_hasRegisteredUserSession() {
		$o = &fta_getSharedRegisteredUserSession();
		$ret = $o->hasSession();
		return $ret;		
	}
	
	function &fta_getSharedRegisteredUserSession () {
		
		global $FTARegisteredUserSessionClass;
				
		//this is a simple registration hook to ensure that the correct subclass files are loaded
		//before sessions
		global $FTARegisteredUserSessionClass;
		if(!is_null($FTARegisteredUserSessionClass)){
			$c = $FTARegisteredUserSessionClass;
		} else {
			$c = "FTARegisteredUserSession";
		}
		
		if(!isset($_SESSION)) {
			session_start();
		}
		
		$o = extractObjectWithKeyOfClassFromArray('FTARegisteredUserSession',$c,$_SESSION,null );
		if (is_null($o)){		
			$o = new $c();			
		}
		
		//we need to remember to stick things back in the session if we fuck about with them
		$_SESSION['FTARegisteredUserSession'] = $o;	
				
		return $o;
	}
	
	function fta_logoutRegisteredUserSession() {
		$o = fta_getSharedRegisteredUserSession();
		$o->logout();		
	}


//	//again.. this needs to be labelled global as we are included inside a magical place.
//	//this set of stuff needs a session.. and if we call stuff that wants to start a session
//	//and content has already be sent.. we break.. so we do this when the class / module is loaded..
//	// we create the FTA object.. stick it in the session and redirect 				
//	global $fta_;	
//	$fta_->startSession();
